package ar.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import ar.Location.Helper.CalculationProvider;
import ar.Location.Helper.StringHelper;
import ar.Location.Map.MyOverlayItem;
import ar.Location.PlaceObject.PlaceCategory;
import ar.camera.glsurfaceview.extend.LocationIconSurfaceView;
import ar.camera.glsurfaceview.extend.MiniMapSurfaceView;
import ar.camera.glsurfaceview.extend.PlaneSurfaceView;
import ar.camera.pojo.GLLocation;
import ar.camera.pojo.GLPoint;
import ar.camera.pojo.GeoLocation;
import ar.camera.renderer.extend.LocationIconRenderer;
import ar.camera.surfaceview.CameraSurfaceView;
import ar.camera.surfaceview.TochSurfaceView;
import ar.globals.Globals;
import ar.sensor.SensorListener;

public class ARCameraActivity extends Activity {

	private TochSurfaceView mTochView;
	private SensorManager mSensorManager;
	private Sensor orientater;
	private SensorListener mSensorListener;
	private LayoutInflater inflater;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		inflater = (LayoutInflater) this
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		Globals.planeView = new PlaneSurfaceView(this);
		addContentView(Globals.planeView, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		Globals.locationIconView = new LocationIconSurfaceView(this, this);
		addContentView(Globals.locationIconView, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		((LocationIconSurfaceView) Globals.locationIconView)
				.setmPlaneRenderer(Globals.planeView.getmRenderer());

		Globals.compassView = new MiniMapSurfaceView(this);
		addContentView(Globals.compassView, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		((LocationIconSurfaceView) Globals.locationIconView)
				.setmCompassRenderer(Globals.compassView.getmRenderer());

		mTochView = new TochSurfaceView(this);
		addContentView(mTochView, new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		mTochView.setmLocationIconView(Globals.locationIconView);

		Globals.arCameraView = new CameraSurfaceView(this, 0);
		addContentView(Globals.arCameraView, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		mSensorListener = new SensorListener(Globals.planeView,
				Globals.locationIconView, Globals.compassView);
		mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
		orientater = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

		Globals.locationIconView.getmRenderer().setmMyCurrentGeoLocation(
				Globals.currentGeoLocation);
		Globals.locationIconView.getmRenderer().setmListOfGeoLocations(
				Globals.listOfGeoLocations);

		Globals.compassView.getmRenderer().setmMyCurrentGeoLocation(
				Globals.currentGeoLocation);
		Globals.compassView.getmRenderer().setmListOfGeoLocations(
				Globals.listOfGeoLocations);

		Globals.vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
	}

	@Override
	protected void onPause() {
		super.onPause();
		Globals.planeView.onPause();
		Globals.locationIconView.onPause();
		Globals.compassView.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();

		Globals.planeView.onResume();
		Globals.locationIconView.onResume();
		Globals.compassView.onResume();

		mSensorManager.registerListener(mSensorListener, orientater,
				SensorManager.SENSOR_DELAY_UI);
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(getApplicationContext());
		inflater.inflate(R.menu.camera_menu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem item = menu.findItem(R.id.mnCameraViewPlane);
		if (Globals.isShowPlane) {
			item.setTitle("Turn OFF the plane");
			item.setIcon(R.drawable.menu_off_plane);
		} else {
			item.setTitle("Turn ON the plane");
			item.setIcon(R.drawable.menu_on_plane);
		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.mnCameraViewPlane:
			if (Globals.isShowPlane) {
				Globals.isShowPlane = false;
				Globals.planeView.setVisibility(GLSurfaceView.INVISIBLE);
			} else {
				Globals.isShowPlane = true;
				Globals.planeView.setVisibility(GLSurfaceView.VISIBLE);
			}
			break;
		case R.id.mnCameraCurrentLocation:
			LocationIconSurfaceView lis = (LocationIconSurfaceView) Globals.locationIconView;
			lis.setMode(lis.getNone());
			GLPoint eye = new GLPoint(0f, 0f, 0f);
			GLPoint cam = new GLPoint(0f, 0f, -1f);
			Globals.planeView.getmRenderer().setCam(cam);
			Globals.planeView.getmRenderer().setEye(eye);
			Globals.locationIconView.getmRenderer().setEye(eye);
			Globals.locationIconView.getmRenderer().setCam(cam);
			Globals.locationIconView.getmRenderer().updateVertices(0f, null,
					null);
			((LocationIconRenderer) Globals.locationIconView.getmRenderer())
					.setUpdateSensor(true);
			Globals.mode = Globals.SHORT_PRESS;
			break;
		default:
			break;
		}
		return true;
	}

	TextView tvTitle;
	TextView tvDes;
	ImageButton ibClose;

	public void viewPopup(GLLocation glLoc, final MyOverlayItem ol) {
		View camPopup = inflater.inflate(R.layout.camera_popup, null, false);
		addContentView(camPopup, new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		if (tvTitle != null || tvDes != null || ibClose != null)
			invisiblePopup();
		tvTitle = (TextView) camPopup
				.findViewById(R.id.tvCameraPlaceDetailTitle);
		tvDes = (TextView) camPopup
				.findViewById(R.id.tvCameraPlaceDetailDescription);
		ibClose = (ImageButton) camPopup.findViewById(R.id.ibCameraClose);
		GeoLocation curr = Globals.currentGeoLocation;
		float distance = CalculationProvider.distFrom(curr.latitude,
				curr.longitude, (float) (ol.getPoint().getLatitudeE6() / 1E6),
				(float) (ol.getPoint().getLongitudeE6() / 1E6));
		tvTitle.setText(glLoc.textInfo + " - "
				+ StringHelper.getStringDistance((int) distance));
		if (ol.place != null) {

			if (ol.place.description.equals(""))
				tvDes.setText(ol.place.address);
			else
				tvDes.setText(ol.place.description);
		} else {
			tvDes.setText(ol.getTitle());
		}
		ibClose.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				invisiblePopup();
			}
		});
		tvDes.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				invisiblePopup();

				PlaceCategory.currentPlace = ol.place;
				if (PlaceCategory.currentPlace != null) {
					Globals.currOverlayItem = null;
					Intent intent = new Intent(getApplicationContext(),
							PlaceDetailActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(),
							"More info is not available", Toast.LENGTH_SHORT)
							.show();
				}
			}
		});
	}

	public void invisiblePopup() {
		tvTitle.setVisibility(TextView.INVISIBLE);
		tvDes.setVisibility(TextView.INVISIBLE);
		ibClose.setVisibility(ImageButton.INVISIBLE);
		tvTitle = null;
		tvDes = null;
		ibClose = null;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		Globals.currOverlayItem = null;
		if (Globals.arCameraView != null) {
			if (Globals.arCameraView.camera != null) {
				Globals.arCameraView.releaseCam();
			}
		}
		mSensorManager.unregisterListener(mSensorListener);
		Globals.isShowPlane = true;
		Globals.isTrackingOnCam = false;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Globals.arCameraView.releaseCam();
	}
}
